package gases.as3.librarys.components.ui.text
{
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.librarys.components.ui.basic.GLabel;
	import gases.as3.librarys.components.ui.control.GScrollBar;
	
	public class GTextArea extends GUIComponents
	{
		public function GTextArea(width:Number = 200,height:Number = 100,size:int=12,color:uint=0x000000)
		{
			_width = width;
			_height = height;
			_size = size;
			_color = color;
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			_text = new TextField();
//			_text.border = true;
			_text.height = _height + 4;
			_text.text = _value;
			if(!_format)
			{
				_format = new TextFormat();
				_format.color = _color;
				_format.size = _size;
				_format.leftMargin = 1;
				_format.leading = 1;
				_format.letterSpacing =  1;
				_format.align =  TextFieldAutoSize.LEFT;
			}
			_text.defaultTextFormat = _format;
			_text.multiline = true;
			_text.wordWrap = true;
			this.addChild(_text);
			
			_scrollBar = new GScrollBar(_width,_height,_text);
			_text.width = _width - _scrollBar.Gwidth + 5;
			_scrollBar.x = _text.width;
			_scrollBar.y = _text.y;
			_scrollBar.visible = false;
			
			this.addChild(_scrollBar);
		}
		
		
		public function set color(value:uint):void
		{
			_color = value;
			_format.color = value;
			setFormat(_format);
			checkState();
		}
		
		public function set size(value:int):void
		{
			_size = value;
			_format.size = value;
			setFormat(_format);
			checkState();
		}
		
		public function set text(str:String):void
		{
			_text.htmlText = str;
			_value = str;
			checkState();
		}
		
		public function set align(value:String):void
		{
			_format.align = value;
			setFormat(_format);
		}
		
		public function get text():String
		{
			return _value;
		}
		
		
		public function setFormat(mat:TextFormat, start:int = -1 , end:int = -1):void
		{
			_format = mat;
			_text.setTextFormat(_format, start, end);
			_text.defaultTextFormat = _format;
		}
		
		/**
		 *检查状态 
		 * 
		 */		
		private function checkState():void
		{
			_text.height = Math.max(_text.textHeight+8,_height);
			if(_text.textHeight > _height)
			{
				_scrollBar.visible = true;
				_scrollBar.updateScroll();
				_scrollBar.px = 20;
			}
			else
			{
				_scrollBar.visible = false;
			}
		}
		
		override public function dispose():void
		{
			super.dispose();
			_format = null;
			if(this.contains(_text))
			{
				this.removeChild(_text);
			}
			if(this.contains(_scrollBar))
			{
				this.removeChild(_scrollBar);
			}
		}
		
		private var _value:String = " ";
		private var _width:Number; 
		private var _height:Number;
		private var _size:int;
		private var _color:uint;
		private var _format:TextFormat;
		
		private var _text:TextField;
		private var _scrollBar:GScrollBar;
	}
}